package com.spdb.spdbfl.database;

import com.mysql.jdbc.StringUtils;
import com.spdb.spdbfl.utils.CommonUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DBOperate {
    private static String connectString = "";
    private static boolean isPrintSql = CommonUtil.isPrintSql();

    public DBOperate() {
    }

    public static String insert(String insertSql) {
        if (StringUtils.isNullOrEmpty(connectString)) {
            connectString = DBInfo.getConn();
        }

        return insert(connectString, insertSql);
    }

    public static int execute(String executeSql) {
        if (StringUtils.isNullOrEmpty(connectString)) {
            connectString = DBInfo.getConn();
        }

        return execute(connectString, executeSql);
    }

    public static List<Map<String, Object>> select(String selectSql) {
        if (StringUtils.isNullOrEmpty(connectString)) {
            connectString = DBInfo.getConn();
        }

        return select(connectString, selectSql);
    }

    public static Map<String, Object> selectOne(String selectSql) {
        if (StringUtils.isNullOrEmpty(connectString)) {
            connectString = DBInfo.getConn();
        }

        return selectOne(connectString, selectSql);
    }

    public static String insert(String connString, String insertSql) {
        if (isPrintSql) {
            System.out.println(insertSql);
        }

        Connection conn = null;
        setConnection(connString);
        String ret_id = "";

        try {
            conn = getConnection();
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(insertSql);
            ResultSet res = stmt.executeQuery("select LAST_INSERT_ID()");
            if (res.next()) {
                ret_id = res.getString(1);
            }
        } catch (Throwable t) {
            throw new RuntimeException("[Database.insert error]" + t.toString());
        } finally {
            if (null != conn) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return ret_id;
    }

    public static int execute(String connString, String insertSql) {
        if (isPrintSql) {
            System.out.println(insertSql);
        }

        Connection conn = null;
        setConnection(connString);

        int ret_id;
        try {
            conn = getConnection();
            Statement stmt = conn.createStatement();
            ret_id = stmt.executeUpdate(insertSql);
        } catch (Throwable t) {
            throw new RuntimeException("[Database.update error]" + t.toString());
        } finally {
            if (null != conn) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return ret_id;
    }

    public static List<Map<String, Object>> select(String connString, String selectSql) {
        List<Map<String, Object>> ll = new ArrayList<Map<String, Object>>();
        if (!StringUtils.isNullOrEmpty(selectSql)) {
            if (isPrintSql) {
                System.out.println(selectSql);
            }

            Connection conn = null;
            setConnection(connString);

            try {
                conn = getConnection();
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(selectSql);
                ResultSetMetaData md = rs.getMetaData();
                int columns = md.getColumnCount();

                while (rs.next()) {
                    Map<String, Object> row = new HashMap<String, Object>();

                    for (int i = 1; i <= columns; ++i) {
                        row.put(md.getColumnLabel(i), rs.getString(i));
                    }

                    ll.add(row);
                }
            } catch (Throwable t) {
                throw new RuntimeException("[Database.select error]" + t.toString());
            } finally {
                if (null != conn) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

            }

        }
        return ll;
    }

    public static Map<String, Object> selectOne(String connString, String selectSql) {
        List<Map<String, Object>> ll = select(connString, selectSql);
        return (Map<String, Object>)(ll.size() > 0 ? (Map<String, Object>)ll.get(0) : new HashMap<String, Object>());
    }

    private static Connection getConnection() {
        Connection conn = null;
        conn = DataSource.getConnection(connectString);
        return conn;
    }

    public static void setConnection(String conn) {
        connectString = conn;
    }



    public static void main(String[] args) {
        String conn = "jdbc:mysql://10.116.2.244:3306/puyin?user=puyin&password=Jason71419831224Wb";
        String sql = "select * from puyin.lease_assets limit 10;";
        List<Map<String, Object>> listMap = DBOperate.select(conn, sql);
        System.out.println("count=" + listMap.size());
    }
}
